package com.my.jli.Service.impl;

import com.my.jli.Service.HomeworkService;
import com.my.jli.dao.HomeWorkAndStudentRepository;
import com.my.jli.dao.HomeworkRepository;
import com.my.jli.entity.Homework;
import com.my.jli.entity.HomeworkAndStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName HomeServiceImpl
 * @Description
 * @Author MaYu
 * @Date 2022/4/6 19:02
 * @Version 1.0
 **/
@Service
public class HomeworkServiceImpl implements HomeworkService {

    private HomeworkRepository homeworkRepository;
    @Autowired
    public void setHomeworkRepository(HomeworkRepository homeworkRepository) {
        this.homeworkRepository = homeworkRepository;
    }

    private HomeWorkAndStudentRepository homeWorkAndStudentRepository;
    @Autowired
    public void setHomeWorkAndStudentRepository(HomeWorkAndStudentRepository homeWorkAndStudentRepository) {
        this.homeWorkAndStudentRepository = homeWorkAndStudentRepository;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Homework> getAll() {
        return homeworkRepository.findAll();
    }

    @Override
    @Transactional(readOnly = true)
    public Page<Homework> getByPage(Integer tid, Pageable pageable) {
        return homeworkRepository.findByTid(tid, pageable);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<Homework> getByPage(Example<Homework> example, Pageable pageable) {
        return homeworkRepository.findAll(example, pageable);
    }

    @Override
    @Transactional(readOnly = true)
    public Homework getById(Integer id) {
        Optional<Homework> homeworkOptional = homeworkRepository.findById(id);
        return homeworkOptional.orElse(null);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void submitHomework(HomeworkAndStudent hs) {
        homeWorkAndStudentRepository.save(hs);
    }

    @Override
    @Transactional(readOnly = true)
    public List<HomeworkAndStudent> getAllHS() {
        return homeWorkAndStudentRepository.findAll();
    }

    @Override
    @Transactional(readOnly = true)
    public HomeworkAndStudent getHSByHidAndSid(Integer hid, Integer sid) {
        return homeWorkAndStudentRepository.findByHidAndSid(hid, sid);
    }

    @Override
    @Transactional(readOnly = true)
    public List<HomeworkAndStudent> getHSByHid(Integer hid) {
        return homeWorkAndStudentRepository.findByHid(hid);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Homework saveHomework(Homework homework) {
        return homeworkRepository.save(homework);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteHomeworkById(Integer hid) {
        homeworkRepository.deleteById(hid);
    }

    @Override
    @Transactional(readOnly = true)
    public int getSubmitCount(Integer hid) {
        return homeWorkAndStudentRepository.countByHid(hid);
    }
}
